package tools

import (
	"encoding/json"
	"strconv"
	"strings"
	"time"
)

func StringToInt64(e string) (int64, error) {
	return strconv.ParseInt(e, 10, 64)
}

func StringToInt(e string) (int, error) {
	return strconv.Atoi(e)
}

func IntToString(e int) string {
	return strconv.Itoa(e)
}

//逗号分隔字符串转[]int
func StringToIntArr(e string) ([]int, error) {
	var intArr []int
	str_arr1 := strings.Split(e, ",")
	for j := 0; j < len(str_arr1); j++ {
		int_data, err := strconv.Atoi(str_arr1[j])
		if err != nil {
			return []int{}, err
		} else {
			intArr = append(intArr, int_data)
		}
	}
	return intArr, nil
}

func StringToBool(e string) (bool, error) {
	return strconv.ParseBool(e)
}

func GetCurrentTimeStr() string {
	return time.Now().Format("2006-01-02 15:04:05")
}

func GetCurrentTime() time.Time {
	return time.Now()
}

func FormatTimeStr(timeStr string) (string, error) {
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation("2006-01-02T15:04:05.000Z", timeStr, loc)
	return theTime.Format("2006/01/02 15:04:05"), err
}

func TimeStampToTimeStr(timeStr string) (string, error) {
	loc, _ := time.LoadLocation("Local")
	if true == strings.Contains(timeStr, "+08:00") {
		timeStr = strings.TrimSuffix(timeStr, "+08:00")
		theTime, err := time.ParseInLocation("2006-01-02T15:04:05", timeStr, loc)
		return theTime.Format("2006/01/02 15:04:05"), err
	} else if true == strings.Contains(timeStr, "Z") {
		theTime, err := time.ParseInLocation("2006-01-02T15:04:05Z", timeStr, loc)
		return theTime.Format("2006/01/02 15:04:05"), err
	}
	return timeStr, nil
}

func TimeStampToTimeUnix(timeStr string) (int64, error) {
	loc, _ := time.LoadLocation("Local")
	if true == strings.Contains(timeStr, "+08:00") {
		timeStr = strings.TrimSuffix(timeStr, "+08:00")
		theTime, err := time.ParseInLocation("2006-01-02T15:04:05", timeStr, loc)
		return theTime.Unix(), err
	} else if true == strings.Contains(timeStr, "Z") {
		theTime, err := time.ParseInLocation("2006-01-02T15:04:05Z", timeStr, loc)
		return theTime.Unix(), err
	}
	return 0, nil
}

func StructToJsonStr(e interface{}) (string, error) {
	if b, err := json.Marshal(e); err == nil {
		return string(b), err
	} else {
		return "", err
	}
}

func MapToJsonStr(e map[string]interface{}) (string, error) {
	if b, err := json.Marshal(e); err == nil {
		return string(b), err
	} else {
		return "", err
	}
}

func JsonStrToMap(e string) (map[string]interface{}, error) {
	var dict map[string]interface{}
	if err := json.Unmarshal([]byte(e), &dict); err == nil {
		return dict, err
	} else {
		return nil, err
	}
}

func JsonStrToStruct(e string) (interface{}, error) {
	var dict interface{}
	if err := json.Unmarshal([]byte(e), &dict); err == nil {
		return dict, err
	} else {
		return nil, err
	}
}

func StructToMap(data interface{}) (map[string]interface{}, error) {
	dataBytes, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}
	mapData := make(map[string]interface{})
	err = json.Unmarshal(dataBytes, &mapData)
	if err != nil {
		return nil, err
	}
	return mapData, nil
}

func StringToIntSlice(users string) []int {
	var int_list []int

	string_list := strings.Split(users, ",")
	for _, user := range string_list {
		user_int, _ := strconv.Atoi(user)
		int_list = append(int_list, user_int)
	}

	return int_list
}

func StringToIntSliceNull(users string) []int {
	var int_list []int
	if users != "" {
		string_list := strings.Split(users, ",")
		for _, user := range string_list {
			user_int, _ := strconv.Atoi(user)
			int_list = append(int_list, user_int)
		}
	}
	return int_list
}

// similar_text()
//var percent float64
//(string1, string2, &percent)
// 计算两个字符串的相似度
func SimilarText(first, second string, percent *float64) int {
	var similarText func(string, string, int, int) int
	similarText = func(str1, str2 string, len1, len2 int) int {
		var sum, max int
		pos1, pos2 := 0, 0

		// Find the longest segment of the same section in two strings
		for i := 0; i < len1; i++ {
			for j := 0; j < len2; j++ {
				for l := 0; (i+l < len1) && (j+l < len2) && (str1[i+l] == str2[j+l]); l++ {
					if l+1 > max {
						max = l + 1
						pos1 = i
						pos2 = j
					}
				}
			}
		}

		if sum = max; sum > 0 {
			if pos1 > 0 && pos2 > 0 {
				sum += similarText(str1, str2, pos1, pos2)
			}
			if (pos1+max < len1) && (pos2+max < len2) {
				s1 := []byte(str1)
				s2 := []byte(str2)
				sum += similarText(string(s1[pos1+max:]), string(s2[pos2+max:]), len1-pos1-max, len2-pos2-max)
			}
		}

		return sum
	}

	l1, l2 := len(first), len(second)
	if l1+l2 == 0 {
		return 0
	}
	sim := similarText(first, second, l1, l2)
	if percent != nil {
		*percent = float64(sim*200) / float64(l1+l2)
	}
	return sim
}

func GetSystemSign() string {
	// 请求路由
	systemSign := "crm"
	return systemSign
}